<?php
 //No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewCurrency extends JView
{
	function display($tpl=null)
	{
		global $mainframe, $option;
		
		//Initialise variables
		$user		=& JFactory::getUser();
		$ownuser	=& Ownservice::getUser();
		$document	=& JFactory::getDocument();
		$db			=& JFactory::getDBO();
		$params 	=& $mainframe->getParams('com_ownservice');
		
		if($user->gid < 25) {
			if (!Ownservice::checkAccess( 'currency', 'manage' )) {
				$mainframe->redirect( 'index.php', JText::_('ALERTNOTAUTH') );
			}
		}

		if($this->getLayout() == 'form') {
			$this->_displayForm($tpl);
			return;
		}

		$filter_order		= $mainframe->getUserStateFromRequest( $option.'.currency.filter_order', 'filter_order', 'c.name', 'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'.currency.filter_order_Dir',	'filter_order_Dir',	'ASC', 'word' );

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/icon.css');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//get data from the model
		$rows		=& $this->get( 'Data' );
		$pageNav	=& $this->get( 'Pagination' ); 
		
		// Create a user access object for the current user
		$access = new stdClass();
		$access->canView		= ($user->gid < 25)? Ownservice::checkAccess('currency','view','currency','all'):1;
		$access->canEdit		= ($user->gid < 25)? Ownservice::checkAccess('currency','edit','currency','all'):1;
		$access->canAdd			= ($user->gid < 25)? Ownservice::checkAccess('currency','add','currency','all'):1;
		$access->canDelete		= ($user->gid < 25)? Ownservice::checkAccess('currency','delete','currency','all'):1;

		$lists = array();

		// table ordering
		$lists['order_Dir'] = $filter_order_Dir;
		$lists['order'] = $filter_order;
		
		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_CURRENCIES'));
		$document->setTitle( $params->get('page_title') );

		//assign data to document
		$this->assignRef('rows', $rows);
		$this->assignRef('pageNav', $pageNav);
		$this->assignRef('lists', $lists);
		$this->assignRef('access', $access);

		parent::display($tpl);
	}

	/**
	 * Creates the currency submit form
	 *
	 * @since 1.0
	 */
	function _displayForm($tpl)
	{
		global $mainframe, $option;
		
		//initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		$gid		= $user->get('gid');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		
		//Get data form the model
		$model		= $this->getModel();
		$row		= &$this->get('Item');
		
		// check if it's an edit action
		if($row->id) {
				$canEdit	= ($gid<25)?Ownservice::checkAccess('currency', 'edit', 'currency', 'all'):1;
				if (!$canEdit) {
					JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
					$mainframe->redirect( 'index.php?option=com_ownservice');
				}
		} else {
				$canAdd		= ($gid<25)?Ownservice::checkAccess('currency', 'add', 'currency', 'all'):1;
				if (!$canAdd) {
					JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
					$mainframe->redirect( 'index.php?option=com_ownservice');
				}
		}

		//set page title
		if($row->id) {
			$params->set('page_title',	JText::_('OWNSERVICE_EDIT_CURRENCY'));
		} else {	
			$params->set('page_title',	JText::_('OWNSERVICE_NEW_CURRENCY'));
		}
		$document->setTitle( $params->get('page_title') );

		$lists = array();

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		
		parent::display($tpl);
	}
}
?>

